var HtmlConsole = (function () {
    function HtmlConsole(container) {
        var _this = this;
        this.settings = new HtmlConsoleSettings();
        this.readers = new Array();
        this.lineCount = 0;
        this.container = container;
        this.text = document.createElement("span");
        this.text.classList.add("console-text");
        container.appendChild(this.text);
        this.input = document.createElement("input");
        this.input.type = "text";
        this.input.classList.add("console-input");
        this.input.id = "console-input";
        container.appendChild(this.input);
        container.onkeydown = function (ev) {
            if (_this.input !== document.activeElement) {
                _this.input.focus();
                _this.input.onkeydown(ev);
            }
        };
        this.input.onkeydown = function (ev) {
            _this.KeyPress(ev);
        };
        this.EndLine();
    }
    Object.defineProperty(HtmlConsole.prototype, "width", {
        get: function () {
            var fontTest = document.createElement("span");
            var testStr = "qwertyuiopasdfghjklzxcvbnm,./;'[]";
            fontTest.innerText = testStr;
            fontTest.style.visibility = "hidden";
            this.container.appendChild(fontTest);
            return $(this.container).width() / ($(fontTest).width() / testStr.length);
        },
        enumerable: true,
        configurable: true
    });

    HtmlConsole.prototype.Focus = function () {
        this.input.focus();
    };

    HtmlConsole.prototype.Write = function (text) {
        var _this = this;
        var lines = text.split("\n");
        lines.forEach(function (line, i) {
            if (i != 0)
                _this.EndLine();
            _this.WriteOnLine(line);
        });
    };

    HtmlConsole.prototype.WriteLine = function (text) {
        this.Write(text);
        this.EndLine();
    };

    HtmlConsole.prototype.WriteOnLine = function (text) {
        //entitize
        var text = text.replace(/[\u00A0-\u9999<>\&\s]/gim, function (i) {
            if (i == " ")
                return "&nbsp;";
            if (i == "\t")
                return "&nbsp;&nbsp;&nbsp;&nbsp;";
            return '&#' + i.charCodeAt(0) + ';';
        });

        //span for the text
        var span = document.createElement("span");
        this.settings.ApplyStyles(span);
        span.innerHTML = text;
        this.text.lastElementChild.appendChild(span);
        var offset = $(span).offset().top + $(span).outerHeight();
        var pos = $(window).scrollTop() + window.innerHeight - offset;
        if (pos < 0)
            window.scrollBy(0, -pos);
    };

    HtmlConsole.prototype.EndLine = function () {
        var separator = document.createElement("div");
        separator.classList.add("separator");
        this.text.appendChild(separator);
        var line = document.createElement("span");
        line.classList.add("line");
        line.id = "line-" + this.lineCount;
        this.text.appendChild(line);
        this.lineCount++;
    };

    HtmlConsole.prototype.KeyPress = function (ev, enter) {
        if (typeof enter === "undefined") { enter = true; }
        if (ev.keyCode == 13 && enter) {
            var val = ev.srcElement.value;
            this.input.value = "";
            this.WriteLine(val);
            this.CallReaders(val);
            this.Write(">");
        }
        this.settings.ApplyStyles(this.input);
        this.input.size = this.input.value.length + 3;
    };

    HtmlConsole.prototype.ReadLine = function (callback) {
        this.readers.push(callback);
    };

    HtmlConsole.prototype.CallReaders = function (text) {
        this.readers = this.readers.filter(function (f) {
            return f(text);
        });
    };

    HtmlConsole.Init = function () {
        var elements = document.getElementsByClassName("console");
        for (var i = 0; i < elements.length; i++) {
            if (elements.item(i) instanceof HTMLDivElement)
                HtmlConsole.Repository.push(new HtmlConsole(elements.item(i)));
        }
    };

    HtmlConsole.Repository = new Array();
    return HtmlConsole;
})();

var HtmlConsoleSettings = (function () {
    function HtmlConsoleSettings() {
    }
    HtmlConsoleSettings.prototype.ApplyStyles = function (element) {
        element.style.color = this.foregroundColor;
        element.style.backgroundColor = this.backgroundColor;
    };
    return HtmlConsoleSettings;
})();
//# sourceMappingURL=console-core.js.map
